# SPDX-License-Identifier: MIT             #
# Copyright (C) 2021-.... Jing Leng        #
# Contact: Jing Leng <lengjingzju@163.com> #
############################################

# 设置cbuild-ng路径
CBUILD_NG_PATH ?= $(realpath ../cbuild-ng)

# 加载cbuild-ng环境配置
ifeq ($(OS),Windows_NT)
	$(shell powershell -Command "Get-Content $(CBUILD_NG_PATH)/scripts/build.env | ForEach-Object { $$_ -replace 'export ','$$env:' } | ForEach-Object { Invoke-Expression $$_ }")
else
	include $(CBUILD_NG_PATH)/scripts/build.env
endif

# 加载ZENrs特定环境配置
ifneq (,$(wildcard $(realpath $(PWD))/scripts/zenrs.env))
	ifeq ($(OS),Windows_NT)
		$(shell powershell -Command "Get-Content $(realpath $(PWD))/scripts/zenrs.env | ForEach-Object { $$_ -replace 'export ','$$env:' } | ForEach-Object { Invoke-Expression $$_ }")
	else
		include $(realpath $(PWD))/scripts/zenrs.env
	endif
endif

WORKDIR        := $(ENV_CFG_ROOT)
CONF_SRC       := $(CBUILD_NG_PATH)/scripts/kconfig
CONF_OUT       := $(WORKDIR)
KCONFIG        := $(WORKDIR)/Kconfig
CONF_SAVE_PATH := $(PWD)/configs
CONF_HEADER    := __ZENRS_GLOBAL_CONFIG__
CONF_MFLAG     := -s $(ENV_BUILD_JOBS)
DEF_CONFIG     := def_config
export CONF_SRC

# 设置环境变量
ENV_TOOL_DIR   := $(CBUILD_NG_PATH)/scripts/bin
ENV_MAKE_DIR   := $(CBUILD_NG_PATH)/scripts/core

IGNORE_DIRS    := .git:.svn:output:.github:boot:docs:ffi_integration:formal_verification
KEYWORDS       := none
MAXLEVEL       := 2

.PHONY: all clean distclean deps all-deps menuconfig defconfig help init_env

all: export MFLAG ?= -s
all: init_env loadconfig
	@$(PRECMD)make $(MFLAG) $(if $(filter y,$(tsflag)),,$(ENV_BUILD_JOBS)) MAKEFLAGS= all_targets
	@echo "ZENrs build done!"

# 支持多种构建模式
zenrs: all

# 单独构建exokernel
exokernel: loadconfig
	@$(PRECMD)make $(MFLAG) $(if $(filter y,$(tsflag)),,$(ENV_BUILD_JOBS)) MAKEFLAGS= exokernel

# 单独构建safe_ffi
safe_ffi: loadconfig
	@$(PRECMD)make $(MFLAG) $(if $(filter y,$(tsflag)),,$(ENV_BUILD_JOBS)) MAKEFLAGS= safe_ffi

# 单独构建safe_services
safe_services: loadconfig safe_ffi
	@$(PRECMD)make $(MFLAG) $(if $(filter y,$(tsflag)),,$(ENV_BUILD_JOBS)) MAKEFLAGS= safe_services

# 环境初始化
init_env:
	@echo "Initializing ZENrs build environment..."
	@if [ -f $(realpath $(PWD))/scripts/zenrs.env ]; then \
		source $(realpath $(PWD))/scripts/zenrs.env && init_build_env || exit 1; \
	fi

# 配置相关命令
menuconfig:
	@$(PRECMD)make $(CONF_MFLAG) O=$(CONF_OUT) -C $(CONF_SRC) menuconfig KCONFIG_CONFIG=$(WORKDIR)/.config

defconfig:
	@mkdir -p $(WORKDIR)
	@cp -f $(CONF_SAVE_PATH)/$(DEF_CONFIG) $(WORKDIR)/.config || echo "No default config found"

loadconfig:
	@if [ ! -f $(WORKDIR)/.config ]; then \
		make defconfig; \
	fi

# 依赖分析
deps:
	@mkdir -p $(WORKDIR)
	@$(PRECMD)python3 $(ENV_TOOL_DIR)/gen_build_chain.py -m $(WORKDIR)/auto.mk -k $(WORKDIR)/Kconfig -t $(WORKDIR)/Target \
		-d mk.deps -v mk.vdeps -c mk.kconf -s $(PWD) -i $(IGNORE_DIRS) -l $(MAXLEVEL) -w $(KEYWORDS)

# 清理命令
clean:
	@rm -rf $(ENV_CROSS_ROOT)/objects $(ENV_CROSS_ROOT)/sysroot $(ENV_NATIVE_ROOT)
	@echo "Clean Done."

distclean:
	@rm -rf $(ENV_CROSS_ROOT) $(ENV_NATIVE_ROOT) $(WORKDIR)
	@echo "Distclean Done."

# 帮助信息
help:
	@echo "ZENrs Build System using cbuild-ng"
	@echo "Usage: make [TARGET] [OPTIONS]"
	@echo ""
	@echo "Main targets:"
	@echo "  all             - Build all components"
	@echo "  exokernel       - Build the Zig exokernel only"
	@echo "  safe_ffi        - Build the safe_ffi component only"
	@echo "  safe_services   - Build the safe_services component only"
	@echo ""
	@echo "Configuration:"
	@echo "  menuconfig      - Run menuconfig to configure the build"
	@echo "  defconfig       - Load default configuration"
	@echo ""
	@echo "Cleaning:"
	@echo "  clean           - Clean build objects"
	@echo "  distclean       - Clean all build artifacts"
	@echo ""
	@echo "Options:"
	@echo "  CBUILD_NG_PATH=path - Set path to cbuild-ng (default: ../cbuild-ng)"
	@echo ""

-include $(WORKDIR)/.config
-include $(WORKDIR)/auto.mk
-include $(ENV_MAKE_DIR)/inc.conf.mk